package com.acme.bankapp.mock;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.service.bank.BankReport;

public class BankServer {
	ServerSocket providerSocket;
	Socket connection = null;
	ObjectOutputStream out;
	ObjectInputStream in;
	String messageFromClient;
	String messageToClient;
	BankReport br = new BankReport();

	public BankServer() {
	}

	void run(Bank bank) {
		try {
			// 1. creating a server socket
			providerSocket = new ServerSocket(2004, 10);
			// 2. Wait for connection
			System.out.println("Waiting for connection");
			connection = providerSocket.accept();
			System.out.println("Connection received from "
					+ connection.getInetAddress().getHostName());
			// 3. get Input and Output streams
			out = new ObjectOutputStream(connection.getOutputStream());
			out.flush();
			in = new ObjectInputStream(connection.getInputStream());
			// 4. The two parts communicate via the input and output streams
			do {
				try {					
					messageFromClient = (String) in.readObject();	
					
					switch (messageFromClient) {
						case "gnc": { //getNbOfClients							
							messageToClient = ("number of clients = "
						+ Integer.toString(br.getNbOfCustomers(bank)));
							break;
						}
						case "gna": { //getNbofAccounts														
							messageToClient = ("number of accounts = "
							+ Integer.toString(br.getNbOfAccounts(bank)));							
							break;
						}
						case "gasbs": { //getAccountsSortedBySum							
							messageToClient = br.getAccountsSortedBySum(bank).toString();	
							break;
						}
						case "gbc": { //getBankCredit
							messageToClient = (Integer.toString(br.getBankCredit(bank)));	
							break;
						}
						case "gca": { //getCustomerAccount
							messageToClient = (br.getCustomerAccount(bank).toString());	
							break;
							}
						case "exit": { //exit
							messageToClient = "bye";
							break;
							}
						default: {
							messageToClient = "the wrong command from user";							
						}						
					}
					sendMessage(messageToClient);
					
				} 
					catch (ClassNotFoundException classnot) {
					System.err.println("Data received in unknown format");
				}
			} while (!messageFromClient.equals("exit"));
		} catch (IOException ioException) {
			ioException.printStackTrace();
		} finally {
			// 4: Closing connection
			try {
				in.close();
				out.close();
				providerSocket.close();
			} catch (IOException ioException) {
				ioException.printStackTrace();
			}
		}
	}

	void sendMessage(final String msg) {
		try {			
			out.writeObject(msg);
			//out.flush();
			//System.out.println("server>" + msg);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public void startServer(Bank bank) {		
		while (true) {
			run(bank);
		}
	}
}
